package cn.iocoder.yudao.module.erp.controller.admin.stock.goodswarehouselink;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.stock.goodswarehouselink.vo.GoodsWarehouseLinkPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.stock.goodswarehouselink.vo.GoodsWarehouseLinkRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.stock.goodswarehouselink.vo.GoodsWarehouseLinkSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.goodswarehouselink.GoodsWarehouseLinkDO;
import cn.iocoder.yudao.module.erp.service.stock.goodswarehouselink.GoodsWarehouseLinkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 库存")
@RestController
@RequestMapping("/erp/inventory")
@Validated
public class GoodsWarehouseLinkController {

    @Resource
    private GoodsWarehouseLinkService goodsWarehouseLinkService;

    @GetMapping("/get-by-link")
    @Operation(summary = "基于warehouseId和goodsId获取对应库存")
    @PreAuthorize("@ss.hasPermission('erp:goods-warehouse-link:query')")
    @Parameters({
            @Parameter(name = "productId", description = "产品编号", example = "10"),
            @Parameter(name = "warehouseId", description = "仓库编号", example = "2")
    })
    public CommonResult<GoodsWarehouseLinkRespVO> getLinkWithWarehouseNamesByGoodsIdWarehouseId(@RequestParam(value = "goodsId") Long goodsId,
                                                                                                @RequestParam(value = "warehouseId") Long warehouseId) {
        return success(goodsWarehouseLinkService.getLinksByGoodsIdWarehouseId(goodsId, warehouseId));
    }

    @PostMapping("/create")
    @Operation(summary = "创建物品和仓库的库存")
    @PreAuthorize("@ss.hasPermission('erp:goods-warehouse-link:create')")
    public CommonResult<Long> createGoodsWarehouseLink(@Valid @RequestBody GoodsWarehouseLinkSaveReqVO createReqVO) {
        return success(goodsWarehouseLinkService.createGoodsWarehouseLink(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物品和仓库的库存")
    @PreAuthorize("@ss.hasPermission('erp:goods-warehouse-link:update')")
    public CommonResult<Boolean> updateGoodsWarehouseLink(@Valid @RequestBody GoodsWarehouseLinkSaveReqVO updateReqVO) {
        goodsWarehouseLinkService.updateGoodsWarehouseLink(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物品和仓库的库存")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:goods-warehouse-link:delete')")
    public CommonResult<Boolean> deleteGoodsWarehouseLink(@RequestParam("id") Long id) {
        goodsWarehouseLinkService.deleteGoodsWarehouseLink(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物品和仓库的库存")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:goods-warehouse-link:query')")
    public CommonResult<GoodsWarehouseLinkRespVO> getGoodsWarehouseLink(@RequestParam("id") Long id) {
        GoodsWarehouseLinkDO goodsWarehouseLink = goodsWarehouseLinkService.getGoodsWarehouseLink(id);
        return success(BeanUtils.toBean(goodsWarehouseLink, GoodsWarehouseLinkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物品和仓库的连接分页")
    @PreAuthorize("@ss.hasPermission('erp:goods-warehouse-link:query')")
    public CommonResult<PageResult<GoodsWarehouseLinkRespVO>> getGoodsWarehouseLinkPage(@Valid GoodsWarehouseLinkPageReqVO pageReqVO) {
        PageResult<GoodsWarehouseLinkDO> pageResult = goodsWarehouseLinkService.getGoodsWarehouseLinkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GoodsWarehouseLinkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物品和仓库的连接 Excel")
    @PreAuthorize("@ss.hasPermission('erp:goods-warehouse-link:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGoodsWarehouseLinkExcel(@Valid GoodsWarehouseLinkPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GoodsWarehouseLinkDO> list = goodsWarehouseLinkService.getGoodsWarehouseLinkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物品和仓库的连接.xls", "数据", GoodsWarehouseLinkRespVO.class,
                        BeanUtils.toBean(list, GoodsWarehouseLinkRespVO.class));
    }

}